import { _decorator, animation, Animation } from "cc";
import { Entity } from "../Entity";
import { DirectionNames } from "../../enums/Direction";
import { CocosUtils } from "../../utils/CocosUtils";
import { EventType } from "../../enums/EventType";

const { ccclass } = _decorator;

@ccclass("AttackState")
export class AttackState extends animation.StateMachineComponent {

    public async onMotionStateEnter(controller: animation.AnimationController, motionStateStatus: Readonly<animation.MotionStateStatus>): Promise<void> {
        const entity = controller.getComponent(Entity);
        const entityName = entity.name;
        const animName = `${entityName}/attack/${DirectionNames[entity.direction]}`;
        const animation = controller.getComponentInChildren(Animation);
        const atlasPath = `texture/${entityName}/attack/${DirectionNames[entity.direction]}/attack`;
        const events = [
            {
                frame: 0.5,
                func: "onTrigger",
                params: [
                    EventType.SCENE_SHAKE,
                    `${entity.direction}`
                ]
            }
        ];
        await CocosUtils.playAnimation(animation, animName, atlasPath, { events });
        entity.state = Entity.State.IDLE;
    }
}